package com.yunshang.shoppingmall.shopping.productApi.controller;

import com.yunshang.shoppingmall.entity.SearchHistory;
import com.yunshang.shoppingmall.entity.UserNo;
import com.yunshang.shoppingmall.service.SearchHistoryService;
import com.yunshang.shoppingmall.service.User.UserRankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
//import org.springframework.data.redis.core.RedisTemplate;
//import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/wap")
public class SearchHistoryController {

    @Autowired
    SearchHistoryService searchHistoryService;
    @Autowired
    UserRankService userRankService;

//    @Autowired
//    RedisTemplate redisTemplate;
//
//    @Autowired
//    StringRedisTemplate stringRedisTemplate;

    @ResponseBody
    @RequestMapping("/addSearchHistory")
    public int addSearchHistory(HttpServletRequest request, @RequestParam("productName") String productName) {
        HttpSession session = request.getSession();
        int row = 0;
        if (session != null) {
            UserNo userNo = (UserNo) session.getAttribute("userNo");
            //   int rankId=userNo.getUserId();
            int userId = 0;
            userId = userNo.getUserId();
            if (userId != 0) {
                SearchHistory searchHistory = new SearchHistory();
                if (productName != null && productName != "") {
                    searchHistory.setShContent(productName);
                }
                Long time = System.currentTimeMillis();
                Timestamp timestamp = new Timestamp(time);
                searchHistory.setShTime(timestamp);

                searchHistory.setUserId(userId);

                row = searchHistoryService.insert(searchHistory);

            }

        }
        return row;
    }

    @ResponseBody
    @RequestMapping("selectSearchHistoryByUserId")
    public Map<String, Object> selectSearchHistoryByUserId(HttpServletRequest request) {
        HttpSession session = request.getSession();
        Map<String, Object> map = new HashMap<>();
        List<SearchHistory> searchHistoryList=null;
        if (session != null) {
            UserNo userNo = (UserNo) session.getAttribute("userNo");
            int userId = 0;
            if(userNo!=null){
                userId = userNo.getUserId();
                searchHistoryList= searchHistoryService.selectSearchHistoryByUserId(userId);
            }


            map.put("searchHistoryList", searchHistoryList);
        }

        return map;
    }

    @ResponseBody
    @RequestMapping("deleteSearchHistoryByUserId")
    public String deleteSearchHistoryByUserId(HttpServletRequest request) {
        HttpSession session = request.getSession();
        Map<String, Object> map = new HashMap<>();
        int row = 0;
        if (session != null) {
            UserNo userNo = (UserNo) session.getAttribute("userNo");
            int userId = userNo.getUserId();
//            int userId=10002;
            row = searchHistoryService.deleteSearchHistoryByUserId(userId);

        }
        if (row > 0) {
            return "success";
        } else {
            return "faile";
        }

    }

}
